package eu.briano.suirvellancecamera;





import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.session.AccessTokenPair;

import eu.briano.suirvellancecamera.service.AcquireService;
import eu.briano.suirvellancecamera.service.UploadService;
import eu.briano.suirvellancecamera.service.UploadService.LocalBinder;

import android.app.*;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;



public class SuirvellanceCameraActivity extends Activity {
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		
		case R.id.sendNow:
			//BLEAH 
			AsyncTask t =  new AsyncTask<Object, Integer, Long>(){
				@Override
				protected Long doInBackground(Object... params) {
					mService.processAndUploadImages();
					return null;
				}
			};
			t.execute();
			return true;
		
		case R.id.stop:
				stopService(new Intent(this, UploadService.class));
				stopService(new Intent(this, AcquireService.class));
			return true;
		case R.id.quit:
			this.finish();
			return true;

		case R.id.dropbox:
			try {
				mDBApi.getSession().startAuthentication(SuirvellanceCameraActivity.this);
			} catch (Exception e) {
				Log.e(Utils.logName,"DropBox error on init:",e);
			}				
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(Utils.logName,"onCreate");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		startService(new Intent(this, UploadService.class));
		startService(new Intent(this, AcquireService.class));
		bindService(new Intent(this, UploadService.class), mConnection, Context.BIND_AUTO_CREATE);
		
		
		
		mDBApi = Utils.dropBoxConnect(this);	
	}

	// only after on stop
	@Override
	protected void onRestart() {
		super.onPause();
		Log.i(Utils.logName,"onRestart");
	}	

	@Override
	protected void onStart() {
		super.onPause();
		Log.i(Utils.logName,"onStart");
	}
	@Override
	protected void onResume() {
		super.onResume();
		Log.i(Utils.logName,"onResume");
		dropBoxPostResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.i(Utils.logName,"onPause");
	}
	@Override
	protected void onStop() {
		super.onStop();
		
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
        		
		Log.i(Utils.logName,"onStop");
	}

	@Override
	protected void onDestroy() {
		super.onStop();
		Log.i(Utils.logName,"onDestroy");
	}





	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		Log.i(Utils.logName,"finish");
	}

	
	private DropboxAPI<AndroidAuthSession> mDBApi;	
	
	private void dropBoxPostResume() {
		if (mDBApi != null && mDBApi.getSession() != null &&
				mDBApi.getSession().authenticationSuccessful()) {
			try {
				mDBApi.getSession().finishAuthentication();
				AccessTokenPair tokens = mDBApi.getSession().getAccessTokenPair();
				SharedPreferences sp =  getSharedPreferences(Utils.yascPreferencesName,MODE_PRIVATE);
				Editor e = sp.edit();
				e.putString(Utils.dropBoxTokenKey   , tokens.key);
				e.putString(Utils.dropBoxTokenSecret, tokens.secret);
				e.commit();
				
				if (mBound)
					mService.reloadDropBoxConf();
				
				
				Log.i(Utils.logName,"DropBox  authenticating ok");
			} catch (IllegalStateException e) {
				Log.i(Utils.logName,"DropBox error authenticating", e);
			}
		}
	}
	
	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
		Log.v(Utils.logName," finalize !");

	}

	UploadService mService;
	boolean mBound = false;
	private ServiceConnection mConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName className,
				IBinder service) {
			LocalBinder binder = (LocalBinder) service;
			mService = binder.getService();
			mBound = true;
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			mBound = false;
		}
	};	





}